import ajax from "./js/business/ajax.js";
import router from "./js/router/router.js";

let page = 1;
init();
function init() {
  $("#registerMenu").on("click", () => router.go("/register"));
  $("#loginMenu").on("click", () => router.go("/login"));
  $("#listMenu").on("click", () => ajax("auth"));
  $("#logout").on("click", () => ajax("logout"));
  $(document).on("loginData", loginDataHandler);
  $(document).on("logout", logoutHandler);
  ajax("auth");
}

function logoutHandler(e) {
  $("#userName").text("未登录");
  $("#logout").css("display", "none");
  router.go("/login");
}

function loginDataHandler(e, data) {
  if (data.result) {
    router.go("/list", data.userList);
    $("#userName").text(data.user);
    $("#logout").css("display", "block");
    $("#listMenu").css("display", "block");
  } else {
    $("#listMenu").css("display", "none");
    router.go("/login");
  }
}

// var str="qwertyuiop"
// var token=require("jsonwebtoken");
// var key=token.sign({user:"xietian",password:"xie12345"},str);
// //
// var s="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoieGlldGlhbiIsInBhc3N3b3JkIjoieGllMTIzNDUiLCJpYXQiOjE2NTA2MTcwODR9.UB95eDJO9EjsJxP_8e0uBRlmwnR4eMepLtlOr1oLBk0";
// var o=token.verify(s,str);
//
